#!/bin/bash
#2017年8月18日21:32:13
#auto create vsftpd for virtual user
#by author www.jfedu.net
########################
CONF_DIR="/etc/vsftpd"
VIR_USER="$*"
SYS_USER="ftpuser"
LOGIN_DB="vsftpd_login"

if [ $# -eq 0 ];then
	echo -e "\033[32m---------------------\033[0m"
	echo -e "\033[32mUsage:{/bin/sh $0 jfedu001 jfedu002|jfedu003}\033[0m"
	exit 0
fi

if [ ! -f $CONF_DIR/vsftpd.conf ];then
	yum install vsftpd* db4* -y
else
	continue
fi

for i in `echo $VIR_USER`
do
grep "$i" $CONF_DIR/${SYS_USER}s.txt
if [ $? -ne 0 ];then
cat>>$CONF_DIR/${SYS_USER}s.txt<<EOF
$i
pwd_$i
EOF
fi
done

db_load -T -t hash -f $CONF_DIR/${SYS_USER}s.txt $CONF_DIR/$LOGIN_DB.db
chmod 700 $CONF_DIR/${SYS_USER}s.txt
chmod 700 $CONF_DIR/$LOGIN_DB.db

cat>/etc/pam.d/vsftpd<<EOF
auth    sufficient      /lib64/security/pam_userdb.so      db=$CONF_DIR/$LOGIN_DB
account sufficient      /lib64/security/pam_userdb.so      db=$CONF_DIR/$LOGIN_DB
EOF
useradd -s /sbin/nologin $SYS_USER

grep "guest_"  $CONF_DIR/vsftpd.conf
if [ $? -ne 0 ];then
cat>>$CONF_DIR/vsftpd.conf<<EOF
guest_enable=YES
guest_username=$SYS_USER
pam_service_name=vsftpd
user_config_dir=$CONF_DIR/vsftpd_user_conf
virtual_use_local_privs=YES
EOF
fi

for j in `echo $VIR_USER`
do
mkdir -p $CONF_DIR/vsftpd_user_conf/
cat>$CONF_DIR/vsftpd_user_conf/$j <<EOF
local_root=/home/$SYS_USER/$j
write_enable=YES
anon_world_readable_only=YES
anon_upload_enable=YES
anon_mkdir_write_enable=YES
EOF
mkdir -p /home/$SYS_USER/$j/
done

chown -R $SYS_USER.$SYS_USER /home/$SYS_USER
service vsftpd restart
